package com.hongchen.wechat.controller;

import com.hongchen.model.ActGame;
import com.hongchen.model.ActGamePrize;
import com.hongchen.model.ActPrize;
import com.hongchen.model.ActUser;
import com.hongchen.wechat.config.Api;
import com.hongchen.wechat.service.RichService;
import com.jfinal.weixin.sdk.api.ApiResult;

import java.util.Date;
import java.util.List;

import static com.jfinal.aop.Enhancer.enhance;

/**
 * Created by 录康哲 on 2018/1/30.
 * 听歌识曲
 */
public class SoundController extends Api {
    private final RichService richService=enhance(RichService.class);
    ActUser actU = new ActUser();//存储用户信息便于使用

    ApiResult apiResult;

    public void index(){
        this.redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId()+"&redirect_uri=http://"+chengDong()+".tunnel.echomod.cn/sound/name&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect");
    }

    public void name(){
        apiResult=userdetail();
        //开关
        boolean ok=false;
        if(apiResult.getStr("openid")!=null){
            String nickName=apiResult.get("nickname");
            String headImg=apiResult.get("headimgurl");
            String tel=apiResult.get("tel");
            int sex=apiResult.getInt("sex");
            String city=apiResult.get("city");
            String openId=apiResult.get("openid");

            if(openId != null){
                actU.setWechatOpenid(openId);
            }
            if(headImg != null){
                actU.setHeadImg(headImg);
            }
            if(tel != null){
                actU.setTel(tel);
            }
            if(nickName != null){
                actU.setWechatNickname(nickName);
            }
            actU.setSex(sex);

            if(city != null){
                actU.setCity(city);
            }

            //判断用户是否已经在数据库存在
            ActUser user = richService.findByOpenid(openId);

            if(user == null){//新增
                actU.setCreateTime(new Date());
                if(actU.save()){
                    ActGame actGame=new ActGame();//表示用户准备玩游戏
                    actGame.setActUserId(actU.getId());
                    actGame.setNowTime(new Date());
                    actGame.save();
                }
            }else{//老用户
                actU.setId(user.getId());
                user = actU;
                user.setUpdateTime(new Date());
                user.update();//更新用户信息成功
            }
            setAttr("openid",openId);
        }else{
            ok=true;
        }

        //进行用户信息判断
        if(ok){
            index();
        }else{
            render("sound.html");
        }
    }

    /**
     * 听歌识曲
     */
    public void SoundHound(){
        String name=getPara("name");//正确歌名
        String music=getPara("music");//用户猜的歌名
        int figure=getParaToInt("figure");//连续答题
        String openid=getPara("openid");

        if(name.equals(music)){
            setAttr("success",true);
            if(figure==5){
                //用户连续答对5次筛子机会+1
                richService.updateSieveChance(richService.findByOpenid(openid).getId());
                setAttr("figure",true);
            }
            if(figure==108){
                //用户连续答对108次中奖并且筛子+2(我就不信他这么闲= =)
                richService.updateSieveChanceTwo(richService.findByOpenid(openid).getId());
                Long prize = 0L;//定义用户中的奖品
                ActGame actg = richService.finds(richService.findByOpenid(openid).getId());//查询用户关联游戏表
                List<ActPrize> apList = richService.typePrize(5);//查询听歌识曲奖品集合
                if(apList != null){
                    ActPrize ap = richService.isPrize(apList);//奖品
                    if(ap != null){
                        ActGamePrize agp = new ActGamePrize();
                        agp.setGameId(actg.getId());
                        agp.setPrizeId(ap.getId());
                        agp.setPrizeNum(richService.lastPrizeNo()); //中奖编号
                        agp.setAwardTime(new Date());//当前中奖时间
                        agp.setAcceptTime(BigWheelController.getFetureDate(7));//生成7天后的领奖截止时间
                        agp.setAcceptAddres("河南省郑州市英协路56号传媒创意中心14层河南新维思广告有限公司 泰坦X号");
                        if(agp.save()){//保存中奖信息
                            if(richService.lessOnePrize(ap.getId()) >= 1){//奖品数量减一
                                prize = ap.getId();
                            }
                        }
                        setAttr("valuep",ap.getPrizeValue());///奖品价值
                        setAttr("namep",ap.getPrizeName());///奖品名
                        setAttr("nop",agp.getPrizeNum());///奖品编号
                        setAttr("big",true);
                    }
                }

            }
        }else if(name != null){
            setAttr("suucess",false);
        }

       /* int rand =  new Random().nextInt(100);//随机数
        if(rand>=50){
            setAttr("centre",true);//代表猜对没中奖
        }else{
            setAttr("success",true);
        }*/

        renderJson();
    }
}
